package com.yzcity.cinema.web.rest;

import com.yzcity.cinema.domain.BaseType;
import com.yzcity.cinema.repository.BaseTypeRepository;
import com.yzcity.cinema.web.rest.errors.BadRequestAlertException;

import io.github.jhipster.web.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing {@link com.yzcity.cinema.domain.BaseType}.
 */
@RestController
@RequestMapping("/api")
@Transactional
public class BaseTypeResource {

    private final Logger log = LoggerFactory.getLogger(BaseTypeResource.class);

    private static final String ENTITY_NAME = "baseType";

    @Value("${jhipster.clientApp.name}")
    private String applicationName;

    private final BaseTypeRepository baseTypeRepository;

    public BaseTypeResource(BaseTypeRepository baseTypeRepository) {
        this.baseTypeRepository = baseTypeRepository;
    }

    /**
     * {@code POST  /base-types} : Create a new baseType.
     *
     * @param baseType the baseType to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new baseType, or with status {@code 400 (Bad Request)} if the baseType has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/base-types")
    public ResponseEntity<BaseType> createBaseType(@Valid @RequestBody BaseType baseType) throws URISyntaxException {
        log.debug("REST request to save BaseType : {}", baseType);
        if (baseType.getId() != null) {
            throw new BadRequestAlertException("A new baseType cannot already have an ID", ENTITY_NAME, "idexists");
        }
        BaseType result = baseTypeRepository.save(baseType);
        return ResponseEntity.created(new URI("/api/base-types/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(applicationName, false, ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * {@code PUT  /base-types} : Updates an existing baseType.
     *
     * @param baseType the baseType to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated baseType,
     * or with status {@code 400 (Bad Request)} if the baseType is not valid,
     * or with status {@code 500 (Internal Server Error)} if the baseType couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/base-types")
    public ResponseEntity<BaseType> updateBaseType(@Valid @RequestBody BaseType baseType) throws URISyntaxException {
        log.debug("REST request to update BaseType : {}", baseType);
        if (baseType.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        BaseType result = baseTypeRepository.save(baseType);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, baseType.getId().toString()))
            .body(result);
    }

    /**
     * {@code GET  /base-types} : get all the baseTypes.
     *
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of baseTypes in body.
     */
    @GetMapping("/base-types")
    public List<BaseType> getAllBaseTypes() {
        log.debug("REST request to get all BaseTypes");
        return baseTypeRepository.findAll();
    }

    /**
     * {@code GET  /base-types/:id} : get the "id" baseType.
     *
     * @param id the id of the baseType to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the baseType, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/base-types/{id}")
    public ResponseEntity<BaseType> getBaseType(@PathVariable Long id) {
        log.debug("REST request to get BaseType : {}", id);
        Optional<BaseType> baseType = baseTypeRepository.findById(id);
        return ResponseUtil.wrapOrNotFound(baseType);
    }

    /**
     * {@code DELETE  /base-types/:id} : delete the "id" baseType.
     *
     * @param id the id of the baseType to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/base-types/{id}")
    public ResponseEntity<Void> deleteBaseType(@PathVariable Long id) {
        log.debug("REST request to delete BaseType : {}", id);
        baseTypeRepository.deleteById(id);
        return ResponseEntity.noContent().headers(HeaderUtil.createEntityDeletionAlert(applicationName, false, ENTITY_NAME, id.toString())).build();
    }
}
